home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 August: Tool Chest / Apple_Developer_CD_Series_August_1997_Tool_Chest.iso / Sample Code / Overview / TESample / TESampleInit.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-18  |  11.4 KB  |  295 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    MultiFinder-Aware Simple TextEdit Sample Application
  6. #
  7. #    TESample
  8. #
  9. #    This file: TESampleInit.c    -    C Source (Init Segment)
  10. #
  11. #    Copyright © 1989 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:
  15. #                1.00                08/88
  16. #                1.01                11/88
  17. #                1.02                04/89
  18. #                1.03                06/89
  19. #                1.04                06/92
  20. #
  21. #    Components:
  22. #                TESample.p            June 1, 1989
  23. #                TESample.c            June 1, 1989
  24. #                TESampleInit.c        June 4, 1992
  25. #                TESampleGlue.a        June 1, 1989
  26. #                TESample.r            June 1, 1989
  27. #                TESample.h            June 1, 1989
  28. #                PTESample.make        June 1, 1989
  29. #                CTESample.make        June 1, 1989
  30. #                TCTESample.π        June 4, 1992
  31. #                TCTESample.π.rsrc    June 4, 1992
  32. #                TCTESampleGlue.c    June 4, 1992
  33. #
  34. #    TESample is an example application that demonstrates how 
  35. #    to initialize the commonly used toolbox managers, operate 
  36. #    successfully under MultiFinder, handle desk accessories and 
  37. #    create, grow, and zoom windows. The fundamental TextEdit 
  38. #    toolbox calls and TextEdit autoscroll are demonstrated. It 
  39. #    also shows how to create and maintain scrollbar controls.
  40. #
  41. #    It does not by any means demonstrate all the techniques you 
  42. #    need for a large application. In particular, Sample does not 
  43. #    cover exception handling, multiple windows/documents, 
  44. #    sophisticated memory management, printing, or undo. All of 
  45. #    these are vital parts of a normal full-sized application.
  46. #
  47. #    This application is an example of the form of a Macintosh 
  48. #    application; it is NOT a template. It is NOT intended to be 
  49. #    used as a foundation for the next world-class, best-selling, 
  50. #    600K application. A stick figure drawing of the human body may 
  51. #    be a good example of the form for a painting, but that does not 
  52. #    mean it should be used as the basis for the next Mona Lisa.
  53. #
  54. #    We recommend that you review this program or Sample before 
  55. #    beginning a new application. Sample is a simple app. which doesn’t 
  56. #    use TextEdit or the Control Manager.
  57. #
  58. ------------------------------------------------------------------------------*/
  59.  
  60.  
  61. /* Segmentation strategy:
  62.  
  63.    This program consists of three segments.
  64.    1. "Main" contains most of the code, including the MPW libraries, and the
  65.       main program.  This segment is in the file Sample.c
  66.    2. "Initialize" contains code that is only used once, during startup, and
  67.       can be unloaded after the program starts.  This segment is in the file
  68.       SampleInit.c.
  69.    3. "%A5Init" is automatically created by the Linker to initialize globals
  70.       for the MPW libraries and is unloaded right away. */
  71.  
  72.  
  73. /* SetPort strategy:
  74.  
  75.    Toolbox routines do not change the current port. In spite of this, in this
  76.    program we use a strategy of calling SetPort whenever we want to draw or
  77.    make calls which depend on the current port. This makes us less vulnerable
  78.    to bugs in other software which might alter the current port (such as the
  79.    bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
  80.    Hopefully, this also makes the routines from this program more self-contained,
  81.    since they don't depend on the current port setting. */
  82.  
  83.  
  84. /* Clipboard strategy:
  85.  
  86.    This program does not maintain a private scrap. Whenever a cut, copy, or paste
  87.    occurs, we import/export from the public scrap to TextEdit's scrap right away,
  88.    using the TEToScrap and TEFromScrap routines. If we did use a private scrap,
  89.    the import/export would be in the activate/deactivate event and suspend/resume
  90.    event routines. */
  91.  
  92. /* A/UX is case sensitive, so use correct case for include file names */
  93. #include <Limits.h>
  94. #include <types.h>
  95. #include <quickdraw.h>
  96. #include <fonts.h>
  97. #include <events.h>
  98. #include <controls.h>
  99. #include <windows.h>
  100. #include <menus.h>
  101. #include <textedit.h>
  102. #include <dialogs.h>
  103. #include <desk.h>
  104. #include <scrap.h>
  105. #include <toolutils.h>
  106. #include <memory.h>
  107. #include <segload.h>
  108. #include <files.h>
  109. #include <osutils.h>
  110. #ifndef AUX
  111. #  include <diskinit.h>
  112. #endif
  113. #include <packages.h>
  114. #include "TESample.h"        /* bring in all the #defines for TESample */
  115. #include <traps.h>
  116.  
  117. /* A/UX C understands neither #pragma, nor segments, so don't pragma */
  118. #ifndef AUX
  119.   #pragma segment Initialize
  120. #endif
  121.  
  122.  
  123. /* The "g" prefix is used to emphasize that a variable is global. */
  124. /* All are extern since the variables are declared in the main segment. */
  125.  
  126. /* GMac is used to hold the result of a SysEnvirons call. This makes
  127.    it convenient for any routine to check the environment. It is
  128.    global information, anyway. */
  129. extern SysEnvRec    gMac;                /* set up by Initialize */
  130.  
  131. /* GHasWaitNextEvent is set at startup, and tells whether the WaitNextEvent
  132.    trap is available. If it is false, we know that we must call GetNextEvent. */
  133. extern Boolean        gHasWaitNextEvent;    /* set up by Initialize */
  134.  
  135. /* GInBackground is maintained by our OSEvent handling routines. Any part of
  136.    the program can check it to find out if it is currently in the background. */
  137. extern Boolean        gInBackground;        /* maintained by Initialize and DoEvent */
  138.  
  139. /* GNumDocuments is used to keep track of how many open documents there are
  140.    at any time. It is maintained by the routines that open and close documents. */
  141. extern short        gNumDocuments;        /* maintained by Initialize, DoNew, and DoCloseWindow */
  142.  
  143.  
  144. /*    Set up the whole world, including global variables, Toolbox managers,
  145.     menus, and a single blank document. */
  146.  
  147. /*    1.01 - The code that used to be part of ForceEnvirons has been moved into
  148.     this module. If an error is detected, instead of merely doing an ExitToShell,
  149.     which leaves the user without much to go on, we call AlertUser, which puts
  150.     up a simple alert that just says an error occurred and then calls ExitToShell.
  151.     Since there is no other cleanup needed at this point if an error is detected,
  152.     this form of error- handling is acceptable. If more sophisticated error recovery
  153.     is needed, an exception mechanism, such as is provided by Signals, can be used. */
  154.  
  155. void Initialize()
  156. {
  157.     Handle    menuBar;
  158.     long    total, contig;
  159.     EventRecord event;
  160.     short    count;
  161.  
  162.     gInBackground = false;
  163.  
  164.     InitGraf((Ptr) &qd.thePort);
  165.     InitFonts();
  166.     InitWindows();
  167.     InitMenus();
  168.     TEInit();
  169.     InitDialogs(nil);
  170.     InitCursor();
  171.  
  172.     /*    Call MPPOpen and ATPLoad at this point to initialize AppleTalk,
  173.          if you are using it. */
  174.     /*    NOTE -- It is no longer necessary, and actually unhealthy, to check
  175.         PortBUse and SPConfig before opening AppleTalk. The drivers are capable
  176.         of checking for port availability themselves. */
  177.     
  178.     /*    This next bit of code is necessary to allow the default button of our
  179.         alert be outlined.
  180.         1.02 - Changed to call EventAvail so that we don't lose some important
  181.         events. */
  182.      
  183.     for (count = 1; count <= 3; count++)
  184.         EventAvail(everyEvent, &event);
  185.     
  186.     /*    Ignore the error returned from SysEnvirons; even if an error occurred,
  187.         the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  188.         call to SysEnvirons by calling it after initializing AppleTalk. */
  189.      
  190.     SysEnvirons(kSysEnvironsVersion, &gMac);
  191.     
  192.     /* Make sure that the machine has at least 128K ROMs. If it doesn't, exit. */
  193.     
  194.     if (gMac.machineType < 0) BigBadError(eWrongMachine);
  195.     
  196.     /*    1.02 - Move TrapAvailable call to after SysEnvirons so that we can tell
  197.         in TrapAvailable if a tool trap value is out of range. */
  198.         
  199.     gHasWaitNextEvent = TrapAvailable(_WaitNextEvent, ToolTrap);
  200.  
  201.     /*    1.01 - We used to make a check for memory at this point by examining ApplLimit,
  202.         ApplicationZone, and StackSpace and comparing that to the minimum size we told
  203.         MultiFinder we needed. This did not work well because it assumed too much about
  204.         the relationship between what we asked MultiFinder for and what we would actually
  205.         get back, as well as how to measure it. Instead, we will use an alternate
  206.         method comprised of two steps. */
  207.      
  208.     /*    It is better to first check the size of the application heap against a value
  209.         that you have determined is the smallest heap the application can reasonably
  210.         work in. This number should be derived by examining the size of the heap that
  211.         is actually provided by MultiFinder when the minimum size requested is used.
  212.         The derivation of the minimum size requested from MultiFinder is described
  213.         in Sample.h. The check should be made because the preferred size can end up
  214.         being set smaller than the minimum size by the user. This extra check acts to
  215.         insure that your application is starting from a solid memory foundation. */
  216.      
  217.     if ((long) GetApplLimit() - (long) ApplicationZone() < kMinHeap) BigBadError(eSmallSize);
  218.     
  219.     /*    Next, make sure that enough memory is free for your application to run. It
  220.         is possible for a situation to arise where the heap may have been of required
  221.         size, but a large scrap was loaded which left too little memory. To check for
  222.         this, call PurgeSpace and compare the result with a value that you have determined
  223.         is the minimum amount of free memory your application needs at initialization.
  224.         This number can be derived several different ways. One way that is fairly
  225.         straightforward is to run the application in the minimum size configuration
  226.         as described previously. Call PurgeSpace at initialization and examine the value
  227.         returned. However, you should make sure that this result is not being modified
  228.         by the scrap's presence. You can do that by calling ZeroScrap before calling
  229.         PurgeSpace. Make sure to remove that call before shipping, though. */
  230.     
  231.     /* ZeroScrap(); */
  232.  
  233. #ifndef AUX
  234.     PurgeSpace(&total, &contig);
  235.     if (total < kMinSpace)
  236.         if (UnloadScrap() != noErr)
  237.             BigBadError(eNoMemory);
  238.         else {
  239.             PurgeSpace(&total, &contig);
  240.             if (total < kMinSpace)
  241.                 BigBadError(eNoMemory);
  242.         }
  243. #endif
  244.  
  245.     /*    The extra benefit to waiting until after the Toolbox Managers have been initialized
  246.         to check memory is that we can now give the user an alert to tell him/her what
  247.         happened. Although it is possible that the memory situation could be worsened by
  248.         displaying an alert, MultiFinder would gracefully exit the application with
  249.         an informative alert if memory became critical. Here we are acting more
  250.         in a preventative manner to avoid future disaster from low-memory problems. */
  251.  
  252.     menuBar = GetNewMBar(rMenuBar);            /* read menus into menu bar */
  253.     if ( menuBar == nil )
  254.                 BigBadError(eNoMemory);
  255.     SetMenuBar(menuBar);                    /* install menus */
  256.     DisposeHandle(menuBar);
  257.     AppendResMenu(GetMenuHandle(mApple), 'DRVR');    /* add DA names to Apple menu */
  258.     DrawMenuBar();
  259.  
  260.     gNumDocuments = 0;
  261.  
  262.     /* do other initialization here */
  263.  
  264.     DoNew();                                /* create a single empty document */
  265. } /*Initialize*/
  266.  
  267.  
  268. /* Used whenever a, like, fully fatal error happens */
  269. void BigBadError(short error)
  270. {
  271.     AlertUser(error);
  272.     ExitToShell();
  273. }
  274.  
  275.  
  276. /*    Check to see if a given trap is implemented. This is only used by the
  277.     Initialize routine in this program, so we put it in the Initialize segment.
  278.     The recommended approach to see if a trap is implemented is to see if
  279.     the address of the trap routine is the same as the address of the
  280.     Unimplemented trap. */
  281. /*    1.02 - Needs to be called after call to SysEnvirons so that it can check
  282.     if a ToolTrap is out of range of a pre-MacII ROM. */
  283.  
  284. Boolean TrapAvailable(short tNumber, TrapType tType)
  285. {
  286.     if ( ( tType == (unsigned char) ToolTrap ) &&
  287.         ( gMac.machineType > envMachUnknown ) &&
  288.         ( gMac.machineType < envMacII ) ) {        /* it's a 512KE, Plus, or SE */
  289.         tNumber = tNumber & 0x03FF;
  290.         if ( tNumber > 0x01FF )                    /* which means the tool traps */
  291.             tNumber = _Unimplemented;            /* only go to 0x01FF */
  292.     }
  293.     return NGetTrapAddress(tNumber, tType) != NGetTrapAddress(_Unimplemented, ToolTrap);
  294. } /*TrapAvailable*/
  295.